<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller;

use Hyperf\Context\ApplicationContext;
use Hyperf\Etcd\KVFactory;
use Hyperf\Etcd\KVInterface;
use Hyperf\View\RenderInterface;
use MongoDB\Client;
use Symfony\Component\Console\Application;

use function Hyperf\Collection\value;

class IndexController extends AbstractController
{
    public function index()
    {
        $user = $this->request->input('user', 'Hyperf');
        $age = $this->request->input('age', 18);
        $method = $this->request->getMethod();

        return [
            'method' => $method,
            'message' => "Hello {$user}.{$age}",
        ];
    }
    public function indexRender(RenderInterface $render){
         return $render->render("index",[
            "name"=>"hello.world。阿松大"
         ]);
    }
    public function indexSetEtcd(){
        $client=ApplicationContext::getContainer()->get(KVInterface::class);
        return json_encode($client->put("app.config",json_encode(
            ["url"=>"http://127.0.0.1","user"=>"admin","pwd"=>"adasdasd2121#%#$"]
        )));
    }
    public function indexGetEtcd(){
        $client=ApplicationContext::getContainer()->get(KVInterface::class);
        return ($client->get("app.config")['pwd']);
    }
    public function testmongo(){
        $mongo=new Client(env("MONGO_URI"));
        $db=$mongo->selectDatabase("shop_live");
        $coll=$db->selectCollection("event_list");
        $room_id=123;
        $member_id=456;
        $oprate_type="join";
        return $coll->insertOne([
            "room_id"=>$room_id,
            "member_id"=>$member_id,
            "oprate_type"=>$oprate_type,
            "ip"=>"192.168.0.1",
            "time"=>time(),
            "time_str"=>date("Y-m-d H:i:s")
        ]);
    }
}
